package com.xiyan.base.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xiyan.base.entity.Comment;
import com.xiyan.base.service.ICommentService;
import com.xiyan.base.utils.ResponseResult;
import com.xiyan.base.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ruiyanglee
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/api/comment")
@Api(value = "Comment api", tags = "评论接口")
public class CommentController {
    private static final Logger log = LoggerFactory.getLogger(CommentController.class);

    @Resource
    private ICommentService commentService;

    @ApiOperation("评论信息列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult list() {
        try {
            List<Comment> comments = commentService.list();
            return ResponseResult.ok().data(comments);
        } catch (Exception e) {
            log.error("list interface exception info {}", e.getMessage());
            return ResponseResult.error().message("评论列表请求异常");
        }
    }

    @ApiOperation("新增评论")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Comment comment) {
        try {
            if (ObjectUtil.isNull(comment)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("评论参数为空");
            }
            commentService.saveOrUpdate(comment);
            return ResponseResult.ok().message("评论新增成功");
        } catch (Exception e) {
            log.error("add interface exception info {}", e.getMessage());
            return ResponseResult.error().message("评论新增失败");
        }
    }

    @ApiOperation("博客信息")
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult findById(@PathVariable("id") String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("id为空");
            }
            Comment comment = commentService.getById(id);
            return ResponseResult.ok().data(comment);
        } catch (Exception e) {
            log.error("findById interface exception info {}", e.getMessage());
            return ResponseResult.error().message("评论信息请求异常");
        }
    }


    @ApiOperation("删除评论信息")
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseResult del(@RequestParam String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("id为空");
            }
            commentService.removeById(id);
            return ResponseResult.ok().message("评论删除成功");
        } catch (Exception e) {
            log.error("del interface exception info {}", e.getMessage());
            return ResponseResult.error().message("评论删除失败");
        }
    }

    @ApiOperation("获取博客下评论列表")
    @RequestMapping(value = "/{blogId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult list(@PathVariable("blogId") String blogId) {
        try {
            if (StrUtil.isEmpty(blogId)) {
                return ResponseResult.error().message("博客id为空");
            }

            List<Comment> comments = commentService.findByBlogId(blogId);
            Map<String, Comment> map = new HashMap<>();
            List<Comment> result = new ArrayList<>();

            for (Comment comment : comments) {
                if (StrUtil.isEmpty(comment.getParentId())) {
                    result.add(comment);
                }
                map.put(comment.getId(), comment);
            }

            for (Comment comment : comments) {
                if (StrUtil.isNotEmpty(comment.getParentId())) {
                    Comment parent = map.get(comment.getParentId());
                    if (CollUtil.isEmpty(parent.getChildComment())) {
                        parent.setChildComment(new ArrayList<Comment>());
                    }
                    parent.getChildComment().add(comment);
                }
            }

            return ResponseResult.ok().data(result);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.error().message("获取评论列表失败");
        }
    }
}

